﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IFSModel;

namespace jobAt24
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            const int addStar = 10;
            using (var dataContext = new IFNDBDataContext())
            {
                try
                {
                    var userTrans = new List<Members_point_tran>();
                    var memberNoti = new List<IFSModel.MemberNotification>();
                    var userLastActive = from user in dataContext.Members
                                         where user.LastActive.Value.AddDays(1) >= DateTime.Now
                                         select new
                                                    {
                                                        user.MemID,
                                                        user.UserName
                                                    };
                    foreach (var u in userLastActive)
                    {
                        var member =
                                           dataContext.Members.Single(prop => prop.MemID == u.MemID);
                        if (member != null)
                        {
                            var rand = new Random();
                            int lottory = rand.Next(1, 10);
                            member.Curent_Star += lottory;
                            member.Updatedate = DateTime.Now;
                            userTrans.Add(new Members_point_tran
                            {
                                MemID = u.MemID,
                                TypeID = 4, //Type 4 is the lottory
                                Createdate = DateTime.Now,
                                Mpt_Cal_Star = lottory,
                                Mpt_Cal_Point = 0,
                                Active = 1
                            });
                            memberNoti.Add(new IFSModel.MemberNotification { message = "คุณได้รับ Lottery ประจำวัน " + lottory + " ดาว", UserName = u.UserName, star = lottory, notiTypeId = 1, isRead = false, isDisable = false, point = 0 });
                        }
                    }

                    dataContext.MemberNotifications.InsertAllOnSubmit(memberNoti);
                    dataContext.Members_point_trans.InsertAllOnSubmit(userTrans);
                    dataContext.SubmitChanges();

                }
                catch (Exception ex)
                {


                }
            }
        }
    }
}
